const express = require('express')
const Router = express.Router();
var multer  = require('multer')
var mkdir = require("../config/mkdir")
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //路径
        let url = req.body.url//前端指定路径
        mkdir.mkdirs(`../data/${url}`,(err)=>{
            // console.log(err)
        })
        cb(null, `./data/${url}`)
    },
    filename: function (req, file, cb) {
        let name  = req.body.name//把命名的给前端决定
        // console.log(name)
        cb(null, name+file.originalname.replace(/.*\./,'.'))
    }
})

var upload = multer({ storage: storage })

Router.post('/files/upload', upload.array('file', 9), function (req, res, next) {
    // req.files 是 `photos` 文件数组的信息
    // req.body 将具有文本域数据，如果存在的话
    res.send({path:req.body.url,filename:req.files[0].filename})
    // res.send(req.files[0].filename)
})

module.exports = Router;